#!/usr/bin/env python

'''Unit tests for parsePolyglot.py'''

import unittest
import parsePolyglot

class Test_parseYAML(unittest.TestCase):
    
    def test_simple(self):
        res = parsePolyglot.parseYAML(''' # comment\ndesc: simple\ntests:\n  - cd: alpha\n    ot: gamma\n# comment''')
        self.assertEqual(res, {'desc':'simple', 'tests':[{'cd':'alpha', 'ot':'gamma'}]})
    
    # - line numbers
    def test_line_numbers(self):
        res = parsePolyglot.parseYAML('''# leading comment\nsecond: True''')
        self.assertEqual(res, {'second':'True'})
        self.assertEqual(res['second'].linenumber, 2)
    
    # - multiline comments
    def test_multiline(self):
        res = parsePolyglot.parseYAML('''value: first line\n       second line''')
        self.assertEqual(res['value'], 'first line\nsecond line')
    
    # - nesting
    def test_list_in_list(self):
        res = parsePolyglot.parseYAML('''- - a\n  - b\n- c''')
        self.assertEqual(res, [['a', 'b'], 'c'])
    def test_dict_in_list(self):
        res = parsePolyglot.parseYAML('''- a: 1\n  b: 2\n- c''')
        self.assertEqual(res, [{'a': '1', 'b': '2'}, 'c'])
    def test_dict_in_dict_a(self):
        res = parsePolyglot.parseYAML('''a:\n  b: 1\n  c: 2\n''')
        self.assertEqual(res, {'a': {'b': '1', 'c': '2'}})
    def test_dict_in_dict_b(self):
        res = parsePolyglot.parseYAML('''a: b: 1\n   c: 2''') # start on same line
        self.assertEqual(res, {'a': {'b': '1', 'c': '2'}})
    def test_mixed_nesting(self):
        res = parsePolyglot.parseYAML('''- alpha:\n   - beta:\n      - gamma: smith''')
        self.assertEqual(res, [{'alpha':[{'beta':[{'gamma':'smith'}]}]}])
        

# == main

if __name__ == '__main__':
    unittest.main(verbosity=2)
